<?php
class Admin_ContactController extends Apps_Controller_Action {

    public function indexAction() {
        $this->_helper->redirector('read');
    }

	public function readAction() {
	    $dbAdapter = Zend_Db_Table::getDefaultAdapter();
	    $select = $dbAdapter
            	    ->select()
            	    ->from('contact', null)
            	    ->columns(array(
            	            'id'        => 'contact.id',
            	            'full_name' => 'contact.full_name',
            	            'email'     => 'contact.email',
            	            'phone'     => 'contact.phone',
            	            'subject'   => 'contact.subject',
            	            'status'    => 'contact.status',
            	            'status_label' => new Zend_Db_Expr('CASE contact.status WHEN 1 THEN "Yes" WHEN 0 THEN "No" END'),
            	    ));

	    $grid = new Grid_Contact_Contact('grid-contact');
	    $grid->setCaption('List Contact');
	    $grid->setSource(new OeZend_Widget_Grid_Source_ZendDbSelect($select));
	    $grid->setView(new OeZend_Widget_Grid_View());
	    $grid->setDisablePaginator();

	    $grid->deploy();

	    $this->view->grid = $grid;
	}

	public function detailAction() {
		$rq = $this->getRequest();
		$contactId = intval($rq->getParam('id'));
		$tbContact = new Model_DbTable_Contact();

		$contact = $tbContact->getContactyById($contactId)->query()->fetch();
		$this->view->contact = $contact;
	}


	public function deleteAction() {
		$this->disableLayout(true, true);

		$rq = $this->_request;
		$contactIds = $rq->getParam('item_checker') ? $rq->getParam('item_checker') : $rq->getParam('id');
		$tbContact = new Model_DbTable_Contact();

		if(is_numeric($contactIds)) {
			$this->_flashMessenger->addMessage(
						$tbContact->safelyDelete(array('id = ?' => $contactIds)) ?
						self::$msgDeleteSuccess :
						self::$msgDeleteError
					);
		} elseif (is_array($contactIds)) {
			foreach ($contactIds as $contactId) {
				$tbContact->safelyDelete(array('id = ?' => $contactId));
			}
			$this->_flashMessenger->addMessage(self::$msgDeleteSuccess);
		} else {
			$this->_flashMessenger->addMessage(self::$msgNoRecordFound);
		}

		$this->_helper->redirector('read');
	}

	public function changeValueAction() {
	    $this->disableLayout(true, true);
	    $rq = $this->_request;
	    $param  = $rq->getParam('p');
	    $id 	= intval($rq->getParam('id'));
	    $tbContact = new Model_DbTable_Contact();
	    if(!($r = $tbContact->fetchRow(array('id = ?' => $id))->toArray())) {
	        echo '<script>window.location=window.location</script>
	        <a style="padding:10px 0;display:block;width:100%;text-align:center" href="'
	        . $this->getBacktrackUrl()
	        . '">Update Error (Record not found), please wait ...</a>';
	    }

	    switch ($param) {
	        case 'public':
	            ($r['status'] == Model_DbTable_Contact::STATUS_ACTIVE)
	            ? $tbContact->safelyUpdate(array('status'  => Model_DbTable_Contact::STATUS_INACTIVE), //unchecked
	            array('id =?'	  => $id))
	            : $tbContact->safelyUpdate(array('status'  => Model_DbTable_Contact::STATUS_ACTIVE), //checked
	            array('id =?'  => $id));
	            break;
	        default: break;
	    }

	    echo '<script>location.href = location.href</script>
	    <a style="padding:10px 0;display:block;width:100%;text-align:center" href="'
	    . $this->getBacktrackUrl()
	    . '">Update successfully, please wait ...</a>';
	}
}